﻿using Max.PH5_2011A.Domain.Shared;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2011A.Domain.Order
{
    /// <summary>
    /// 订单主表
    /// </summary>
    [Table("tb_order_master")]
    public class OrderMasterModel : AuditField<Guid>
    {
        /*
        //超市收银
        //谁什么时候买的，一共花了多少钱，用什么付的，谁负责收钱，收了多少钱。
        //会员卡号为：156946这个用户，今天2023/6/5 12:23:00，一共花了101.02，用支付宝付的
        //工号5639的员工操作的，2023/6/5 12:24:25收款99.00，2.02用的积分折现
        */

        //订单编号 
        [Required]
        public string OrderNo { get; set; }

        //会员ID
        public Guid MemberId { get; set; }

        //订单总金额
        public decimal Total { get; set; }

        //下单时间
        public DateTime OrderTime { get; set; } = System.DateTime.Now;

        //支付时间
        public DateTime? PayTime { get; set; }

        //支付金额
        public decimal? PayTotal { get; set; }

        //折扣金额
        public decimal? Rate { get; set; }

        //支付方式
        public OrderPayType? PayType { get; set; }

        //订单状态
        public OrderState OrderState { get; set; } = OrderState.WaitingPay;

        //收货人信息(姓名、电话、地址、备注)
        public string RecivedName { get; set; }
        public string RecivedPhone { get; set; }
        public string RecivedAddress { get; set; }
        public string? Remark { get; set; }


        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <param name="type">表示是1=全球唯一标识符、2=雪花算法、3=业务编号 </param>
        /// <param name="phone_four">业务编号中的手机号后4位</param>
        /// <returns></returns>
        private string CreateOrderNo(int type, string? phone_four = "")
        {
            string orderNo = "";
            switch (type)
            {
                case 1:
                    orderNo = Guid.NewGuid().ToString("N");
                    break;
                case 2:
                    orderNo = new Snowflake.Core.IdWorker(1, 1).NextId().ToString();
                    break;
                default:
                    orderNo = $"P{System.DateTime.Now.ToString("yyyyMMdd")}{phone_four}{new Random().Next(1, 9999).ToString().PadLeft(4,'0')}";
                    break;
            }

            return orderNo;
        }

        /// <summary>
        /// 初始化值
        /// </summary>
        public OrderMasterModel()
        {
            this.OrderNo = this.CreateOrderNo(3);
        }
    }
}
